﻿using System;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;

namespace Client
{
    public partial class ClientConfig : Form
    {
        private String Nick, Pass, ConfPass, Err;
        private UInt16 Port;
        private IPAddress IpAd;
        private IPHostEntry host;
        public ClientConfig()
        {
            InitializeComponent();
        }
        /// <summary>
        /// get the nickname
        /// </summary>
        public String getNick
        {
            get { return Nick; }
        }
        /// <summary>
        /// get the password
        /// </summary>
        public String getPassword
        {
            get { return Pass; }
        }
        /// <summary>
        /// get the port number
        /// </summary>
        public UInt16 getPort
        {
            get { return Port; }
        }
        /// <summary>
        /// get the IP address
        /// </summary>
        public IPAddress getIp
        {
            get { return IpAd; }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Err = "";
            Nick = UserBox.Text;
            if ((IPControl()==true) && (PortControl() == true) && (!(Nick.Equals(""))) && (PassControl()==true))
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                if (Err.Length == 0 ) 
                {
                    Err += "Username value ";
                }
                MessageBox.Show(Err + "error", "Error");
            }        
        }
        /// <summary>
        /// cosa si deve fare alla pressione del bottone cancel, ovvero terminare il programma
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e) {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            HostBox.Enabled = true;
            IpBox1.Enabled = false;
            IpBox2.Enabled = false;
            IpBox3.Enabled = false;
            IpBox4.Enabled = false;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            HostBox.Enabled = false;
            IpBox1.Enabled = true;
            IpBox2.Enabled = true;
            IpBox3.Enabled = true;
            IpBox4.Enabled = true;
        }
        private bool IPControl()
        {
            if (HostName.Checked)
            {
                String hos = HostBox.Text;
                if (!hos.Equals(""))
                {
                    try
                    {
                        host = Dns.GetHostEntry(hos);
                        IpAd = Array.Find(host.AddressList, a => a.AddressFamily == AddressFamily.InterNetwork);//<-- così prende il primo ipv4 //host.AddressList[0];  
                    }
                    catch
                    {
                        Err += "Host Name ";
                        return false;
                    }
                }
                else
                {
                    Err += "Host Name value ";
                    return false;
                }
            }
            else if (IpAddr.Checked)
            {
                String temp = IpBox1.Text + "." + IpBox2.Text + "." + IpBox3.Text + "." + IpBox4.Text;
                if (!temp.Equals("..."))
                {
                    try
                    {
                        IpAd = IPAddress.Parse(temp);
                    }
                    catch
                    {
                        Err += "IP Address value ";
                        return false;
                    }
                }
                else
                {
                    Err += "IP Address undefined ";
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// controlla la validità della porta inserita (valore tra 1023 e 65535)
        /// </summary>
        /// <param name="po">porta del server inserita da input</param>
        /// <returns></returns>
        private bool PortControl() {
            String porta = PortBox.Text;
            if (!porta.Equals(""))
            {
                try
                {
                    Port = UInt16.Parse(porta);
                }
                catch
                {
                    Err += "Port value ";
                    return false;
                }
            }
            else 
            {
                Err += "Port value ";
                return false;
            }
            if ((Port > 1023) && (Port <= 65535))
            {
                return true;
            }
            else {
                Err += "Port value (must be between 1024 and 65535) ";
                return false;
            }
        }
        /// <summary>
        /// controllo che i campi password e conferma password siano pieni e siano uguali tra di loro
        /// </summary>
        /// <param name="Pas1">Password</param>
        /// <param name="Pas2">Conferma Password</param>
        /// <returns></returns>
        private bool PassControl() {
            Pass = PassBox1.Text;
            ConfPass = PassBox2.Text;
            if (!(Pass.Equals("")))
            {
                if (!(ConfPass.Equals(""))) 
                {
                    if (Pass.Equals(ConfPass)) 
                    {
                        return true;
                    }
                }
            }
            Err += "Password value ";
            return false;
        }
    }
}
